*******************************************************************************
* 4 Prepare the HSW proxy interview data for wave 3

use "$data_input/OWWA_Proxy HH_Wave 3.dta", clear

* First cleaning
missings dropvars, force
compress

* Recode yes/no answers
local employer "hh_c1_description_employer_good_"
local gift "hh_c19_dried_mangoes_3 hh_c20_dried_mangoes_employer_3"

lab define l_yesno 1 "Yes" 0 "No"

* Hsw received dried mangoes
gen remember_gift_3 = hh_c19_dried_mangoes_3
gen gave_gift_3=hh_c20_dried_mangoes_employer_3
lab val remember_gift_3 gave_gift_3

lab var remember_gift_3 "Remember gift treatment"
lab var gave_gift_3 "Gave gift to the employer"

* Change employers
gen h_change_emp_3 = hh_a2_change_emp_3
lab var h_change_emp_3 "HSW changed employer"
lab val h_change_emp_3 l_yesno

* Generate date of departure (not used)
gen leave_date_3 = .c
replace leave_date_3=hh_a1_date_3 //some of the dates are in 2015/16
format %td leave_date_3
lab var leave_date_3 "Date of departure"

* End date of the FIRST contract
gen end_firstcontract_3=.c
replace end_firstcontract_3= hh_a4_date_change_emp_3
format %td end_firstcontract_3
lab var end_firstcontract_3 "End date of first employment contract"

* End date of CURRENT contract
gen end_currentcontract_3=.c
replace end_currentcontract_3=hh_a7_end_contract_date_3
format %td end_currentcontract_3
lab var end_currentcontract_3 "End date of current employment contract"

* First change of employers date 
gen date_changeemp_3=.c
replace date_changeemp_3= hh_a3_date_change_emp_full_3
format %td date_changeemp_3
lab var date_changeemp_3 "Date when HSW first changed employers" 

* Communication
* Frequency of text messaging
gen text_freq_3=7 if hh_b1_text_family_3==1
replace text_freq_3=5 if hh_b1_text_family_3==2
replace text_freq_3=2.5 if hh_b1_text_family_3==3
replace text_freq_3=1 if hh_b1_text_family_3==4
replace text_freq_3=0 if hh_b1_text_family_3==5

* Number of calls in the past four weeks
gen call_freq_3 = hh_b2_talk_family_times_3

* Length of last phone call in minutes
* Some other values appear too large
destring hh_b3_length_conversation_len_3, gen(call_length_3)
* Some other values appear too large (Cutpoint: 3 hours)
replace call_length_3=.c if call_length_3>180

lab var text_freq_3 "Text frequency (in a week)"
lab var call_freq_3 "Call frequency (in a week)"
lab var call_length_3 "Length of phone conversation (in minutes)"

* Treatment by employer
* Employer described as good
* NOTE: THE VARIABLE NAMES ARE TO LONG AND CUT. THUS, RAW VARIABLES ARE NOT IDENTIFIED AS W2 OR W3
rename hh_c1_description_employer_good_ employer_good_3
rename hh_c1_description_employer_short employer_short_3
rename hh_c1_description_employer_slave employer_slaver_3
rename hh_c1_description_employer_viole employer_violent_3
rename hh_c1_description_employer_stric employer_strict_3

lab val employer_*_3 l_yesno
lab var employer_good_ "Employer described as good"

gen noemployer_short_3 = 1 - employer_short_3
gen noemployer_slaver_3 = 1 - employer_slaver_3
gen noemployer_violent_3 = 1 - employer_violent_3
gen noemployer_strict_3 = 1 - employer_strict_3

lab val noemployer_*_3 employer_*_3 l_yesno

gen employer_subj_index_3 = employer_good_3 + noemployer_short_3 + noemployer_slaver_3 + noemployer_violent_3 + noemployer_strict_3

* Treatment by employer
gen noshouting_3=hh_c4_shout_3_slice 
gen noviolence_3 = hh_c6_violence_3_slice
gen nothreat_3 = hh_c7_threat_3_slice
gen nosexharass_3 = hh_c8_sexharass_3_slice
gen no_near_sexharass_3 = hh_c9_exp_close_3
gen no_worksick_3 = hh_c5_work_sick_3_slice

recode noshouting_3 noviolence_3 nothreat_3 nosexharass_3 no_near_sexharass_3 no_worksick_3 (0=1) (1=0) (98 99=.)
lab val noshouting_3 noviolence_3 nothreat_3 nosexharass_3 no_near_sexharass_3 no_worksick_3 l_yesno

lab var noshouting_3 "Experienced no shouting"
lab var noviolence_3 "Experienced no violence"
lab var nothreat_3 "Experienced no threats"
lab var nosexharass_3 "Experienced no sexual harassment"
lab var no_near_sexharass_3 "No surrounding persons experienced physical violence or sexual harassment"
lab var no_worksick_3 "Not forced to work when sick"

* Daily working hours

* start time
gen double work_start_3=hms(hh_c10_worktime_start_time_hh_3, hh_c10_worktime_start_time_mm_3,0)
format %tcHH:MM work_start_3
lab var work_start_3 "Time HSW start working"

* end time
gen double work_end_3=hms( hh_c11_worktime_end_time_hh_3, hh_c11_worktime_end_time_mm_3,0)
format %tcHH:MM work_end_3
lab var work_end_3 "Time HSW end working"

* If work_end is 00:00, I changed it to 23:59
replace work_end_3=clock("23:59", "hm") if work_end_3==clock("00:00","hm")
*if work_end is between past 12mn to 4am, add 24 hrs
replace work_end_3=work_end_3+24*60*60*1000 if hh(work_end_3)<=4


recode work_end_3 work_start_3 (nonmissing=.) if work_start_3>work_end_3

* compute working hours (not including break)
gen workhours_3=(work_end_3-work_start_3)/(60*60*1000)
format %tcHH:MM workhours_3
lab var workhours_3 "Gross working hours"

**
* generate working hours (include break time)
gen breaktime_3=.c
replace breaktime_3=0 if hh_c12_work_break_3==0 // hsw has no break
replace breaktime_3=clock(hh_c13_hours_break_time_3,"hm") if hh_c12_work_break_3==1
format %tCHH:MM breaktime_3
gen worktime_3=workhours_3-hours(breaktime_3)

replace worktime_3=.c if worktime_3<=0 | worktime_3>=24
lab var worktime_3 "Net working hours"
gen sparetime_3 = 24-worktime_3
lab var sparetime_3 "Non-working hours"

* Weekly rest day
lab list hh_c14_restday_3
gen restday_3=.c
replace restday_3=1 if hh_c14_restday_3==1
replace restday_3=0 if hh_c14_restday_3!=1
replace restday_3=.c if hh_c14_restday_3==.c

lab var restday_3 "HSW has rest day once a week"
lab val  restday_3 l_yesno

* Can leave employer's house during rest day
gen leavehouse_3=.c
replace leavehouse_3=1 if hh_c15_leave_restday_3==1
replace leavehouse_3=0 if hh_c15_leave_restday_3==0
replace leavehouse_3=0 if restday_3==0 //no rest day

lab var leavehouse_3 "HSW can leave the employer's house"
lab val leavehouse_3 l_yesno

* Receive salary on time
gen salary_ontime_3=.c
replace salary_ontime_3=1 if hh_d2_regular_salary_3==1
replace salary_ontime_3=0 if hh_d2_regular_salary_3==2|hh_d2_regular_salary_3==3| hh_d2_regular_salary_3==4 // included 1-7 days delay which is ok 
replace salary_ontime_3=.a if hh_d2_regular_salary_3==.a
replace salary_ontime_3=.b if hh_d2_regular_salary_3==.b
lab var salary_ontime_3 "HSW receives her salary on time"
lab val salary_ontime_3 l_yesno

* Does not experience salary deduction
* Note: In HK, allowed to deduct 10% of first month's salary
gen sal_noded_3=.c
replace sal_noded_3=1 if hh_d3_deduction_3==3
replace sal_noded_3=0 if hh_d3_deduction_3==1|hh_d3_deduction_3==2
replace sal_noded_3=.a if hh_d3_deduction_3==.a
replace sal_noded_3=.b if hh_d3_deduction_3==.b

lab var sal_noded_3 "HSW did not experience salary deduction"
lab val sal_noded_3 l_yesno

* Confiscate mobile phone
gen conf_phone_3=.c
replace conf_phone_3=1 if hh_b4_confiscate_cel_3==1
replace conf_phone_3=0 if hh_b4_confiscate_cel_3==2
lab var conf_phone_3 "Employer attempted to confiscate phone"
lab val conf_phone_3 l_yesno

* Relation to employer
gen employer_rel_3=hh_c2_satisfy_emp_before_3
lab var employer_rel_3 "Quality of relationship with employer (start)"

gen employer_rel_now_3 = hh_c3_satisfy_emp_current_3
lab var employer_rel_now_3 "Quality of relationship with employer (now)"

gen employer_rel_av_3 = (employer_rel_3+employer_rel_now_3)/2
lab var employer_rel_av_3 "Quality of relationship with employer (average)"

* Account and mode of remitting
recode hh_d9_acct_abroad_3 (0=0), gen(account_fo_3)
recode hh_d8_acct_ph_3 (0=0), gen(account_ph_3)
egen account_3 = rowmax(account_fo_3 account_ph_3)

lab var account_fo_3 "Has personal bank account abroad"
lab var account_ph_3 "Has personal bank account in Phil"
lab val account_fo_3 account_ph_3 account_3 l_yesno

*replace . with .c
qui ds, has(type numeric)
local x=r(varlist)
recode `x' (.=.c) 

save "$data_prep/owwa_proxy_w3_edited", replace









